package com.example.hxpc.guidancemanagement.fragment;


import android.content.Intent;
import android.os.Bundle;
        import android.support.annotation.Nullable;
        import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.activity.SendInformationActivity;
import com.example.hxpc.guidancemanagement.adapter.MyRecycleViewAdapter;
import com.example.hxpc.guidancemanagement.base.ActivityInfo;
import com.example.hxpc.guidancemanagement.base.HonorInfo;
import com.example.hxpc.guidancemanagement.base.ProjectInfo;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.ToastTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tao on 2017/11/29.
 * 主页面-活动Frament
 */

public class MainActFrament extends Fragment implements View.OnClickListener{
    private View rootView;
    private TextView te_foeget_pass , te_sure;
    private ImageView img_back , img_add;
    private RecyclerView recyclerView;
    private MyRecycleViewAdapter myRecycleViewAdapter;
    private List<ActivityInfo> aDatas = new ArrayList<>();
    private Intent intent;
    private ArrayList<String> listUrls = null;
    private String content;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.frament_main_activity,null);
        }
        initView(rootView);
        return rootView;
    }

    //初始化控件
    public void initView(View rootView){
        aDatas.clear();
        img_add = (ImageView) rootView.findViewById(R.id.add_mark);
        img_add.setOnClickListener(this);
        img_back = (ImageView) rootView.findViewById(R.id.back);
        img_back.setVisibility(View.GONE);
        te_foeget_pass = (TextView) rootView.findViewById(R.id.te_title);
        te_foeget_pass.setText("活动");
        te_sure = (TextView) rootView.findViewById(R.id.sure);
        te_sure.setVisibility(View.GONE);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.activity_recyclerView);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setStackFromEnd(true);//列表在底部开始展开，反转后由上面开始展开
        layoutManager.setReverseLayout(true);//列表翻转
        recyclerView.setLayoutManager(layoutManager);

        myRecycleViewAdapter = new MyRecycleViewAdapter(getActivity(),aDatas,true);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
        myRecycleViewAdapter.addFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.recyclerview_headerview,null));
        recyclerView.setAdapter(myRecycleViewAdapter);
        myRecycleViewAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                ToastTools.toastMsg(getActivity(),"店家了");
            }

            @Override
            public void onItemLongOnClick(View view, int position) {
                ToastTools.toastMsg(getActivity(),"ooooooooo");
            }
        });
//        myRecycleViewAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
//            @Override
//            public void OnItemClick(View view, int position) {
//                switch (view.getId()){
//                    case R.id.activity_te_content:
//                        LogoutT.logout("点击了名字");
//                        LogoutT.logout("当前的位置是"+position);
//                        break;
//                }
//            }
//        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_mark:
                intent = new Intent(getActivity(), SendInformationActivity.class);
                startActivityForResult(intent , 0);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1){
            if (requestCode == 0){
                listUrls = data.getStringArrayListExtra("photo");
                content = data.getStringExtra("content");
                LogoutT.logout("photo"+listUrls);
                LogoutT.logout("content"+content);
                listUrls.remove(listUrls.size()-1);

                ActivityInfo info = new ActivityInfo("哦哦",content,R.drawable.logo,listUrls);
                aDatas.add(info);
                if (aDatas.size()>2){
                    myRecycleViewAdapter.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.recyclerview_footerview,null));
                }
                myRecycleViewAdapter.notifyDataSetChanged();
            }
        }
    }
}
